package com.booksystem.campuslibrarymanagementsystem.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.booksystem.campuslibrarymanagementsystem.commen.request.userquest.BookBorrowrequst.Approval;
import com.booksystem.campuslibrarymanagementsystem.commen.request.userquest.BookReturnrequst.Returnbook;
import com.booksystem.campuslibrarymanagementsystem.commen.result.userresuilt.ReturnTable;
import com.booksystem.campuslibrarymanagementsystem.entity.BorrowApprovalsTable;
import com.booksystem.campuslibrarymanagementsystem.entity.ReservationTable;
import com.booksystem.campuslibrarymanagementsystem.entity.ReturnrequesTable;
import org.apache.ibatis.annotations.*;

import java.time.LocalDate;
import java.util.List;

@Mapper
public interface ReturnrequesTableMapper extends BaseMapper<ReturnrequesTable> {
    //进行归还
    @Insert("INSERT INTO returnreques_table (bookid, returndate, approvalstate, account) VALUES (#{bookid}, #{returndate}, #{approvalstate}, #{account})")
    int add(ReturnrequesTable cc);

    //判断是否重复归还
    @Select("SELECT * \n" +
            "FROM returnreques_table \n" +
            "WHERE account = #{account} AND bookid = #{bookid}")
    List<ReturnrequesTable> existsByAccountAndBookidi(String account, int bookid);

    //归还用户id查看所有列表
    @Select("select * from returnreques_table where account = #{account}")
    List<ReturnTable> returnlist(@Param("account") String account);

    //通过状态是否通过查看归还列表
    @Select("select * from returnreques_table where account=#{account} and approvalstate = #{approvalstate}")
    List<ReturnTable> returnliststatus(String account, String approvalstate);
    //通过图书id查看借阅图书是否存在记录,已经通过借阅才能归还图书？
    @Select("select * from borrowapprovals_table where account = #{account} and bookid = #{bookid} ")
    List<BorrowApprovalsTable> ddd(@Param("account") String account, @Param("bookid") int bookid);


    //删除该账户的归还记录
    @Delete("DELETE FROM returnreques_table WHERE account = #{account} AND returnid = #{returnid}")
    int deleteByAccountAndBookid(@Param("account") String account, @Param("returnid") int returnid);
    //通过账户id查询借阅记录，获得该账户借阅记录
    @Select("SELECT * FROM returnreques_table\n" +
            "WHERE accountid =  #{accountid} ")
    List<ReturnrequesTable> queryByAccount(String account);

    //通过借阅审批成功，更新借阅审批表的该审批审批的状态，时间和日期
    @Update("UPDATE returnreques_table SET approvaldate = #{approvaltime}, approvalstate = '通过' WHERE account = #{account} And bookid = #{bookid}")
    void onapproval(@Param("account") String account, @Param("approvalTime") LocalDate approvalTime , @Param("bookid") String bookid,@Param("account") String Approvalstate);
    //通过借阅审批失败，更新借阅审批表的该审批审批的状态，时间和日期
    @Update("UPDATE returnreques_table SET approvaldate = #{approvaltime}, approvalstate = '不通过' WHERE account = #{account} And bookid = #{bookid}")
    void unapproval(@Param("account") String account, @Param("approvalTime") LocalDate approvalTime , @Param("bookid") String bookid,@Param("account") String Approvalstate);
    //    查询账户人员信息
    @Select("select * from returnreques_table where approvalstate = #{state} ")
    List<ReturnrequesTable> querydislist(String approvalstate);

}

